//
//  LiveViewController.m
//  LiveEducation
//
//  Created by Liu,Qingcan on 2017/4/24.
//  Copyright © 2017年 Baidu. All rights reserved.
//

#import "LiveViewController.h"
//#import "LFLiveKit.h"
//#import <IJKMediaFramework/IJKMediaFramework.h>

@interface LiveViewController ()
@property (nonatomic, strong) NSString *liveUrl;

//@property (nonatomic,strong)IJKFFMoviePlayerController *player;

@end

@implementation LiveViewController

+ (instancetype)initWithLiveUrl:(NSString *)str{
    return [[self alloc]initWithLiveUrl:str];
}

- (instancetype)initWithLiveUrl:(NSString *)str{
    self = [super init];
    if (self) {
        self.liveUrl = str;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor lightGrayColor];
//    self.title = @"正在直播";
    
    [self setupPlayer];
//    [self readyForPaly];
    [self setupNavBar];
    
}




- (void)setupNavBar{
//    UINavigationItem *stopItem = [[UINavigationItem alloc]init];
    UIBarButtonItem *stopItem = [[UIBarButtonItem alloc]initWithTitle:@"暂停" style:UIBarButtonItemStyleDone target:self action:@selector(stopBtnClicked:)];
    UIBarButtonItem *resumeItem = [[UIBarButtonItem alloc]initWithTitle:@"继续" style:UIBarButtonItemStyleDone target:self action:@selector(resumeBtnClicked:)];
    self.navigationItem.rightBarButtonItems = @[stopItem,resumeItem];

}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.player play];
    self.tabBarController.tabBar.hidden = YES;
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];

    [self.player stop];
}





- (void)setupPlayer{

    PLPlayerOption *option = [PLPlayerOption defaultOption];
    [option setOptionValue:@15 forKey:PLPlayerOptionKeyTimeoutIntervalForMediaPackets];
    //播放url
    NSURL *url = [NSURL URLWithString:self.liveUrl];
//    self.player.URL = url;
    //播放本地文件
    //    NSURL *url = [[NSBundle mainBundle] URLForResource:@"本地文件" withExtension:nil];
//    self.player = [PLPlayer playerWithURL:url option:option];
    self.player = [[PLPlayer alloc]initWithURL:url option:option];
    self.player.delegate = self;
    self.player.playerView.frame = CGRectMake(0, 64, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height - 64);
    self.player.playerView.backgroundColor = [UIColor whiteColor];

//    self.player.autoReconnectEnable = YES;

    [self.view addSubview:self.player.playerView];
}


- (void)stopBtnClicked:(UIButton *)sender{
    [self.player pause];
}

- (void)resumeBtnClicked:(UIButton *)sender{
    [self.player resume];
}


- (void)player:(nonnull PLPlayer *)player stoppedWithError:(nullable NSError *)error {
    // 当发生错误，停止播放时，会回调这个方法
}
- (void)player:(nonnull PLPlayer *)player codecError:(nonnull NSError *)error {
    // 当解码器发生错误时，会回调这个方法
    // 当 videotoolbox 硬解初始化或解码出错时
    // error.code 值为 PLPlayerErrorHWCodecInitFailed/PLPlayerErrorHWDecodeFailed
    // 播发器也将自动切换成软解，继续播放
}




#pragma mark setter

- (void)setLiveUrl:(NSString *)liveUrl{
    if (!liveUrl) {
        return;
    }
    _liveUrl = liveUrl;
//    _liveUrl = @"rtmp://player.daniulive.com:1935/hls/1222";
}

//- (void)viewWillAppear:(BOOL)animated{
//    [super viewWillAppear:animated];
//    [self startLive];
//}
//
//- (void)viewWillDisappear:(BOOL)animated{
//    [super viewWillDisappear:animated];
//
//    [self stopLive];
//}


//- (LFLiveSession*)session {
//    if (!_session) {
//        _session = [[LFLiveSession alloc] initWithAudioConfiguration:[LFLiveAudioConfiguration defaultConfiguration] videoConfiguration:[LFLiveVideoConfiguration defaultConfiguration]];
//        _session.preView = self;
//        _session.delegate = self;
//    }
//    return _session;
//}
//
//- (void)startLive {
//    LFLiveStreamInfo *streamInfo = [LFLiveStreamInfo new];
//    streamInfo.url = self.liveUrl;
//    [self.session startLive:streamInfo];
//}
//
//- (void)stopLive {
//    [self.session stopLive];
//}



/** 七牛的sdk ，播放带id链接的会失败*/
 
 //- (void)viewWillAppear:(BOOL)animated{
 //    [super viewWillAppear:animated];
 //    [self.player play];
 //    self.tabBarController.tabBar.hidden = YES;
 //}
 //
 //- (void)viewWillDisappear:(BOOL)animated{
 //    [super viewWillDisappear:animated];
 //
 //    [self.player stop];
 //}
 //
 //
 //
 //
 //
 //- (void)setupPlayer{
 //
 //    PLPlayerOption *option = [PLPlayerOption defaultOption];
 //    [option setOptionValue:@15 forKey:PLPlayerOptionKeyTimeoutIntervalForMediaPackets];
 //    //播放url
 //    NSURL *url = [NSURL URLWithString:self.liveUrl];
 //    //播放本地文件
 //    //    NSURL *url = [[NSBundle mainBundle] URLForResource:@"本地文件" withExtension:nil];
 //    self.player = [PLPlayer playerWithURL:url option:option];
 //    self.player.delegate = self;
 //    self.player.playerView.frame = CGRectMake(0, 64, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height - 64);
 //    self.player.playerView.backgroundColor = [UIColor whiteColor];
 //
 //    self.player.autoReconnectEnable = YES;
 //
 //    [self.view addSubview:self.player.playerView];
 //}
 
 //
 //- (void)stopBtnClicked:(UIButton *)sender{
 //    [self.player pause];
 //}
 //
 //- (void)resumeBtnClicked:(UIButton *)sender{
 //    [self.player resume];
 //}

 






/**使用哔哩哔哩的sdk播放 （延迟较高）*/

//
//
//
    //1. 根据当前环境设置日志信息
    //1.1如果是Debug状态的
//- (void)readyForPaly{
//    #ifdef DEBUG
//    //  设置报告日志
//    [IJKFFMoviePlayerController setLogReport:YES];
//    //  设置日志的级别为Debug
//    [IJKFFMoviePlayerController setLogLevel:k_IJK_LOG_DEBUG];
//    //1.2否则(如果不是debug状态的)
//    #else
//    //  设置不报告日志
//    [IJKFFMoviePlayerController setLogReport:NO];
//    //  设置日志级别为信息
//    [IJKFFMoviePlayerController setLogLevel:k_IJK_LOG_INFO];
//    #endif
//
//    // 2. 检查版本是否匹配
//    [IJKFFMoviePlayerController checkIfFFmpegVersionMatch:YES];
//    // 3.  创建IJKFFMoviePlayerController
//    // 3.1 默认选项配置
//    IJKFFOptions *options = [IJKFFOptions optionsByDefault];
//    // 3.2 创建播放控制器
//    IJKFFMoviePlayerController *ffMoviePlayerController = [[IJKFFMoviePlayerController alloc] initWithContentURLString:@"rtmp://player.daniulive.com:1935/hls/stream/" withOptions: options];
////    @"http://swf.ws.126.net/openplayer/v01/-0-2_MBVJRCP13_MBVJRKIDD-vimg1_ws_126_net//image/snapshot_movie/2016/9/6/1/MBVJRKB61-1430711943278.swf"
//    //http://swf.ws.126.net/openplayer/v01/-0-2_M74HRKM0R_M751BMUL9-vimg1_ws_126_net//image/snapshot_movie/2011/6/I/U/M751BMRIU-1430711943278.swf
//    //rtmp://player.daniulive.com:1935/hls/stream
//    //rtmp://192.168.41.35:1935/gzhm/room
//
//    self.player = ffMoviePlayerController;
//    //4. 屏幕适配
//    // 4.1 设置播放视频视图的frame与控制器的View的bounds一致
//    self.player.view.frame = self.view.bounds;
//    // 4.2 设置适配横竖屏(设置四边固定,长宽灵活)
//    self.player.view.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
//    //  4.3 设置播放视图的缩放模式
//    self.player.scalingMode = IJKMPMovieScalingModeAspectFit;
//    //  4.4 设置自动播放
//    self.player.shouldAutoplay = YES;
//    //  4.5 自动更新子视图的大小
//    self.view.autoresizesSubviews = YES;
//    //  4.6 添加播放视图到控制器的View
//    [self.view addSubview:self.player.view];
//    [self.player play];
//
//}
//
//#pragma mark - 视图即将显示
//- (void)viewWillAppear:(BOOL)animated {
//    [super viewWillAppear:animated];
//    //  当试图即将展示的时候开始播放
//    [self.player prepareToPlay];
////    [self.player play];
//}
//#pragma mark - 视图已经消失
//- (void)viewDidDisappear:(BOOL)animated {
//    [super viewDidDisappear:animated];
//    //  关闭播放
//    [self.player shutdown];
////    [self.player stop];
//}



@end
